package com.atguigu.b_arrays_sort;

import java.util.Arrays;
import java.util.Comparator;

public class Demo {
    public static void main(String[] args) {
        //基本数据类型元素数组
        int[] arr = {3,1,4,5,2};

        Arrays.sort(arr); //快速排序

        System.out.println("Arrays.toString(arr) = " + Arrays.toString(arr));

        System.out.println("-------------------");
        //引用数据类型元素数组
        Student[] stus = {
                new Student("张三",18),
                new Student("李四",17),
                new Student("王五",19),
                new Student("张三",18),
                new Student("李四",17),
                new Student("王五",19)
        };

        Arrays.sort(stus); //要元素提供排序规则 -> 绑定比较器提供规则
        System.out.println("Arrays.toString(stus) = " + Arrays.toString(stus));

        //独立比较器对Student[]元素进行排序
        Arrays.sort(stus, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getAge() - o2.getAge();//年龄升序
            }
        });
        System.out.println("Arrays.toString(stus) = " + Arrays.toString(stus));

    }
}
